import 'package:equatable/equatable.dart';

class TopicModel extends Equatable {
  final int id;
  final String title;
  final String description;
  final int creatorId;
  final UserModel creator;
  final String category;
  final List<String> tags;
  final String? aiSummary;
  final double credibilityScore;
  final int viewCount;
  final int likeCount;
  final int commentCount;
  final int shareCount;
  final String status;
  final DateTime createdAt;
  final DateTime updatedAt;

  const TopicModel({
    required this.id,
    required this.title,
    required this.description,
    required this.creatorId,
    required this.creator,
    required this.category,
    required this.tags,
    this.aiSummary,
    required this.credibilityScore,
    required this.viewCount,
    required this.likeCount,
    required this.commentCount,
    required this.shareCount,
    required this.status,
    required this.createdAt,
    required this.updatedAt,
  });

  factory TopicModel.fromJson(Map<String, dynamic> json) {
    return TopicModel(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      creatorId: json['creator_id'],
      creator: UserModel.fromJson(json['creator']),
      category: json['category'],
      tags: List<String>.from(json['tags'] ?? []),
      aiSummary: json['ai_summary'],
      credibilityScore: (json['credibility_score'] ?? 0.0).toDouble(),
      viewCount: json['view_count'] ?? 0,
      likeCount: json['like_count'] ?? 0,
      commentCount: json['comment_count'] ?? 0,
      shareCount: json['share_count'] ?? 0,
      status: json['status'] ?? 'active',
      createdAt: DateTime.parse(json['created_at']),
      updatedAt: DateTime.parse(json['updated_at']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'creator_id': creatorId,
      'creator': creator.toJson(),
      'category': category,
      'tags': tags,
      'ai_summary': aiSummary,
      'credibility_score': credibilityScore,
      'view_count': viewCount,
      'like_count': likeCount,
      'comment_count': commentCount,
      'share_count': shareCount,
      'status': status,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  TopicModel copyWith({
    int? id,
    String? title,
    String? description,
    int? creatorId,
    UserModel? creator,
    String? category,
    List<String>? tags,
    String? aiSummary,
    double? credibilityScore,
    int? viewCount,
    int? likeCount,
    int? commentCount,
    int? shareCount,
    String? status,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return TopicModel(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      creatorId: creatorId ?? this.creatorId,
      creator: creator ?? this.creator,
      category: category ?? this.category,
      tags: tags ?? this.tags,
      aiSummary: aiSummary ?? this.aiSummary,
      credibilityScore: credibilityScore ?? this.credibilityScore,
      viewCount: viewCount ?? this.viewCount,
      likeCount: likeCount ?? this.likeCount,
      commentCount: commentCount ?? this.commentCount,
      shareCount: shareCount ?? this.shareCount,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  List<Object?> get props => [
        id,
        title,
        description,
        creatorId,
        creator,
        category,
        tags,
        aiSummary,
        credibilityScore,
        viewCount,
        likeCount,
        commentCount,
        shareCount,
        status,
        createdAt,
        updatedAt,
      ];
}

// Import UserModel if not already imported
import 'user_model.dart';